package com.ihisomp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ihisomp.base.Page;
import com.ihisomp.base.Result;
import com.ihisomp.dto.OrgDeptDto;
import com.ihisomp.entity.OrgDept;
import com.ihisomp.entity.OrgDeptToFunction;
import com.ihisomp.mapper.OrgDeptMapper;
import com.ihisomp.service.IOrgDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihisomp.service.IOrgDeptToFunctionService;
import com.ihisomp.service.ISysGetNoService;
import com.ihisomp.util.ComUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 科室信息 服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-03-21
 */
@Service
public class OrgDeptServiceImpl extends ServiceImpl<OrgDeptMapper, OrgDept> implements IOrgDeptService {

    @Autowired
    private ISysGetNoService sysGetNoService;

    @Autowired
    private IOrgDeptToFunctionService orgDeptToFunctionService;


    /**
     * 机构科室列表
     *
     * @param orgDeptDto
     * @return
     */
    @Override
    public Result list(OrgDeptDto orgDeptDto) {
        if (orgDeptDto == null) {
            orgDeptDto = new OrgDeptDto();
        }
        QueryWrapper<OrgDept> queryWrapper = new QueryWrapper();
        queryWrapper.eq("org_key", orgDeptDto.getOrgKey());
        queryWrapper.eq("active_flg", 1);
        queryWrapper.orderByAsc("seq_no");
        List<OrgDept> list = baseMapper.selectList(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param orgDeptDto
     * @return
     */
    @Override
    public Result search(long page, long size, OrgDeptDto orgDeptDto) {
        if (orgDeptDto == null) {
            orgDeptDto = new OrgDeptDto();
        }
        // 分页查询
        IPage<OrgDeptDto> data = baseMapper.searchPage(new Page<OrgDeptDto>(page, size), orgDeptDto);
        return Result.ok(data);
    }

    /**
     * 新增数据
     *
     * @param orgDeptDto
     * @return
     */
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    @Override
    public Result add(OrgDeptDto orgDeptDto) {
        if (orgDeptDto == null || StringUtils.isBlank(orgDeptDto.getOrgKey())) {
            return Result.error("机构代码不能为空！");
        }
        if (StringUtils.isBlank(orgDeptDto.getDeptName())) {
            return Result.error("科室名称不能为空！");
        }
        OrgDept orgDept = new OrgDept();
        BeanUtils.copyProperties(orgDeptDto, orgDept);
        String deptKey = sysGetNoService.getSysNo("t_org_dept");
        orgDept.setDeptKey(deptKey);
        orgDept.setActiveFlg(1);
        if (baseMapper.insert(orgDept) > 0) {
            if( orgDeptDto.getDeptFunctions() != null){
                // 新增科室职能
                OrgDeptToFunction orgDeptToFunction = new OrgDeptToFunction();
                orgDeptToFunction.setDeptKey(deptKey);
                orgDeptToFunction.setOrgKey(orgDeptDto.getOrgKey());
                for (int i = 0; i < orgDeptDto.getDeptFunctions().size(); i++) {
                    orgDeptToFunction.setDeptToFunctionKey(ComUtil.createPrimaryKey());
                    orgDeptToFunction.setDeptFunctionKey(orgDeptDto.getDeptFunctions().get(i));
                    if (!orgDeptToFunctionService.save(orgDeptToFunction)) {
                        return Result.error("新增科室信息失败！");
                    }
                }
            }
            return Result.ok();
        } else {
            return Result.error("新增科室信息失败！");
        }
    }


    /**
     * 更新数据
     *
     * @param deptKey
     * @param orgDeptDto
     * @return
     */
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    @Override
    public Result update(String deptKey, OrgDeptDto orgDeptDto) {
        OrgDept orgDept = baseMapper.selectById(orgDeptDto.getDeptKey());
        if (orgDept == null) {
            return Result.error("未查询到科室信息！");
        }
        BeanUtils.copyProperties(orgDeptDto, orgDept);
        if (baseMapper.updateById(orgDept) > 0) {
            OrgDeptToFunction orgDeptToFunction = new OrgDeptToFunction();
            orgDeptToFunction.setDeptKey(deptKey);
            orgDeptToFunction.setOrgKey(orgDeptDto.getOrgKey());
            // 删除科室职能
            if (orgDeptToFunctionService.deleteByDeptKey(deptKey) < 0) {
                return Result.error("删除科室职能失败！");
            }
            // 新增科室职能
            for (int i = 0; i < orgDeptDto.getDeptFunctions().size(); i++) {
                orgDeptToFunction.setDeptToFunctionKey(ComUtil.createPrimaryKey());
                orgDeptToFunction.setDeptFunctionKey(orgDeptDto.getDeptFunctions().get(i));
                if (!orgDeptToFunctionService.save(orgDeptToFunction)) {
                    return Result.error("新增科室职能失败！");
                }
            }
            return Result.ok();
        } else {
            return Result.error("更新科室信息失败！");
        }
    }

    /**
     * 查询数据
     *
     * @param deptKey
     * @return
     */
    @Override
    public Result get(String deptKey) {
        OrgDeptDto orgDeptDto = new OrgDeptDto();
        BeanUtils.copyProperties(baseMapper.selectById(deptKey), orgDeptDto);
        orgDeptDto.setDeptFunctions(orgDeptToFunctionService.getFunctionByDeptKey(deptKey));
        return Result.ok(orgDeptDto);
    }

    /**
     * 更新状态
     *
     * @param deptKey
     * @param activeFlg
     * @return
     */
    @Override
    public Result updateStatus(String deptKey, int activeFlg) {
        OrgDept orgDept = baseMapper.selectById(deptKey);
        if (orgDept == null) {
            return Result.error("无查询科室信息");
        }
        if (activeFlg == 0) {
            orgDept.setActiveFlg(1);
        } else {
            orgDept.setActiveFlg(0);
        }
        if (baseMapper.updateById(orgDept) > 0) {
            return Result.ok();
        }
        return Result.error("更新科室状态失败！");
    }

}
